 clear%To write the source current files 
% Used to make source file with Neetu's new data
%latest date 18JULY06
MyOutPath = 'c:\manoj\ocean\Source\180x360\tsunami\';
%MyOutPath = 'c:\manoj\temp\';
load c:\manoj\projects\tsunami\newvel.mat IU_inter_spec IV_inter_spec;
L = isnan(IU_inter_spec) | isnan(IV_inter_spec);
IU_inter_spec(L) = 0;
IV_inter_spec(L) = 0;
% IU_inter_spec = IU_inter_spec/300;% These lines were used to
% IV_inter_spec = IV_inter_spec/300; % just test the scaling

% 19.JULY.2006 The newvel.mat contains variables
% IU,IV (new data 1x1 sen by Neetu
% IU_inter,IV_inter (interpolated to center)
% IU_inter_spec,IV_inter_spec Spectra files
% lat1,lon1 orginal grid lat2 lon2 interploated grid
load c:\manoj\projects\tsunami\Bz;
Bz = rot90(Bz);
data_ur = zeros([180,360]);
data_ui = zeros([180,360]);
data_vr = zeros([180,360]);
data_vi = zeros([180,360]);
prd = 1./(fftfrq(600,1/(60/(24*3600))));
prd = [4000. prd]; % The mean flow period -> 4000 days (it should have been DC but 4000 days OK)!

if isdir(MyOutPath) == 0,
    mkdir('c:\manoj\ocean\','Source\180x360\tsunami\');
end;
fp = fopen('c:\manoj\temp\a1.project','wt');
for i = 1:301,
    IU1 = squeeze(IU_inter_spec(:,:,i));
    IUr = rot90(real(IU1));
    data_ur(62:121,31:121) = IUr;
    
    IUi = rot90(imag(IU1));
    data_ui(62:121,31:121) = IUi;
    
    IV1 = squeeze(IV_inter_spec(:,:,i));
    IVr = rot90(real(IV1));
    data_vr(62:121,31:121) = IVr;
    
    IVi = rot90(imag(IV1));
    data_vi(62:121,31:121) = IVi;
    
%Note: 26July06. The original grid Neetu (new comp) gave was
%
    
CUr = -data_vr.*Bz.*1e-9.*3.2;
CUi = -data_vi.*Bz.*1e-9.*3.2;
CVr = -data_ur.*Bz.*1e-9.*3.2;
CVi = -data_ui.*Bz.*1e-9.*3.2;

%CU = CU*1000; % Compression for e format 20 oct 2005
%CV = CV*1000; % This multiplication is compensated by scale factor in source file
fprintf(fp,'%s\n',['    ' sprintf('%10.5f',prd(i)) '     ' sprintf('tsu%d',i) ' 1x1_mantle_k3.model ' sprintf('tsu%d',i) '.source' ' 1']);
fid = fopen([MyOutPath sprintf('tsu%d',i) '.source'],'wt');
fid1 = fopen('c:\manoj\ocean\x3dg\header.txt','rt');
%the following format for alexei's x3dg is tested ok 20 oct 05
           for ii = 1:34,
                s=fgetl(fid1);
                fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
for in = 1:180,
    for jn = 1:360,
       fprintf(fid,'%12.4e\n',CUr(in,jn));
   end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
for in = 1:180,
    for jn = 1:360,
        fprintf(fid,'%12.4e\n',CUi(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
 for in = 1:180,
    for jn = 1:360,       
        fprintf(fid,'%12.4e\n',CVr(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:17,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
  
 for in = 1:180,
    for jn = 1:360,       
        fprintf(fid,'%12.4e\n',CVi(in,jn));
    end;
end;
            fprintf(fid,'\n');
            for ii = 1:37,
            s=fgetl(fid1);
            fprintf(fid,'%s\n',s);
            end;
            s=fgetl(fid1);
            fclose(fid);
            fclose(fid1);
end;
fclose all;